/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016/11/24 by CL $
*/
//美化滚动条
function _scroll($box) {
	$("#"+$box).mCustomScrollbar({
		scrollButtons:{
			enable:true
		},
		theme:"dark-2"
	});
}

$(function(){
	/*
	$("body").click(function(){
		if ($("#current_selbox").val().length > 0) {
			_clickEvent("0");
		}
    });
	*/

	//弹出sel box
	$(document).on("click", "[f='but_sel_var']", function(){
		$_box = $(this).attr("data-box");
		$_obj_box = $("#"+$_box);
		

		if ($_obj_box.css("display") == "block") {
			$(this).parent().removeClass("current");
			//$_obj_box.hide();
			$_obj_box.slideUp(100);
			$("#current_selbox").val("");
		}
		else {
			//$_obj_box.show();
			if ($_obj_box.html().length > 0) {
				$(this).parent().addClass("current");
				$("#current_selbox").val($_box);
				$_obj_box.slideDown(100, function(){
					if (!$_obj_box.hasClass("mCustomScrollbar")) {
						_scroll($_box);
					}
					_clickEvent();
				});
			}
		}
	});

	//选中sel box
	$(document).on("click", "[f='but_sel_confirm']", function(){
		$_value = $(this).attr("data-value");
		$_text = $(this).attr("data-text");
		$_input = $(this).attr("data-input");
		$_data_box = $(this).attr("data-box");

		$("#"+$_input).val($_value);
		$("#"+$_data_box).parent().find("span").html($_text);
		$("#"+$_data_box).parent().removeClass("current");
		$("#"+$_data_box).hide();

		//$(this).parent().removeClass("current");
	});

	//选中模拟地区 联动
	$(document).on("click", "[f='but_sel_area']", function(){
		$_value = $(this).attr("data-value");
		$_text = $(this).attr("data-text");
		$_name = $(this).attr("data-name");
		$_depth = $(this).attr("data-depth");
		$_child = $(this).attr("data-child");
		$_depth = parseInt($_depth);

		$("#"+$_name+$_depth).val($_value);
		$_dist_box = $("#"+$_name+$_depth+"_box");
		$_dist_box.parent().find("span").html($_text);
		$_dist_box.parent().removeClass("current");
		$_dist_box.hide();
		

		if ($_child == "1") { //获取子地区

			//清空depth以上的文本
			if ($_depth == 1) {
				$("#"+$_name+($_depth+1)+"_box").html(""); //二级
				$("#"+$_name+($_depth+1)+"_box").parent().find("span").html("市/区");
				$("#"+$_name+($_depth+1)).val("");

				$("#"+$_name+($_depth+2)+"_box").html(""); //三级
				$("#"+$_name+($_depth+2)+"_box").parent().find("span").html("区/县");
				$("#"+$_name+($_depth+2)).val("");

			}
			else if ($_depth == 2) {
				$("#"+$_name+($_depth+1)+"_box").html(""); //三级
				$("#"+$_name+($_depth+1)+"_box").parent().find("span").html("区/县");
				$("#"+$_name+($_depth+1)).val("");
			}

			//获取子地区
			$.ajax({
				type: "POST",
				url: _ROOT_PATH+"index.php",
				cache: false,
				data: {m:"index", c:"area", a:"child", parentid:$_value, depth:$_depth, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$option_html = "";
					$.each($data, function($entryIndex, $obj){
						$option_html += "<li f='but_sel_area'"+
										" data-value='"+$obj.areaid+"'"+
										" data-text='"+$obj.areaname+"'"+
										" data-name='"+$_name+"'"+
										" data-depth='"+($_depth+1)+"'"+
										" data-child='1'>"+$obj.areaname+"</li>";
					});

					//HTML
					$child_name = $_name+""+($_depth+1)+"_box";
					if ($option_html.length > 0) {
						$("#"+$child_name).html("<ul>"+$option_html+"</ul>");
					}
					else {
						$("#"+$child_name).html("");
					}

				},
				error: function() { }
			});


		}
	});


});
function _clickEvent($check) {
	if (typeof($check) == "undefined") {
		$check = "1";
	}
    $(".drop").each(function(){
		$_box = $(this).attr("id");
		$_current = $("#current_selbox").val();
		if ($check == "1") {
			if ($_box != $_current) {
				$(this).hide();
				$(this).parent().removeClass("current");
			}
		}
		else {
			$(this).hide();
			$(this).parent().removeClass("current");
		}
    });
}
